/**
 * @Time: 2018/11/22 16:52
 * @Author: wangmin
 * @File: object
 * @Software: GoLand
 */
package cluster

import (
	"open_manjing/microsys/jsonio"
	"open_manjing/oss/models/block"
	"open_manjing/oss/component/paging"
	"open_manjing/oss/models"
	"github.com/astaxie/beego/orm"
	"strings"
	"open_manjing/oss/models/object"
)

//对象管理
type Object struct {
	App
}

// @Title 对象列表
// @Description 对象列表
// @Param Page     body int     true   "页码"
// @Param PageSize body int     false  "每页数量20-1000"
// @Param Block    body string  false  "块名称"
// @Param Name     body string  false  "文件名称（支持以Name开头的模糊查询）"
// @Param Mime     body string  false  "文件Mime（查询不区分大小写）"
// @Param Ext      body string  false  "文件扩展名（查询不区分大小写）"
// @Success 0     {}
// @Failure 50000 业务错误
// @Failure 50010 请求参数错误
// @router /list [post]
func (c *Object) List() {
	var data struct {
		Page     int
		PageSize int
		Block    string
		Name     string
		Mime     string
		Ext      string
	}
	ok, info := jsonio.Input(c.Ctx.Input.RequestBody, &data)
	if !ok {
		c.Data["json"] = jsonio.Fail(jsonio.ErrParamError, info, nil)
		return
	}
	o := orm.NewOrm()
	qs := o.QueryTable("Object").Filter("Status", 1).Filter("IsDel", 0)
	if data.Block != "" {
		b := &models.Block{}
		o.QueryTable("Block").Filter("Name", data.Block).One(b, "Id")
		qs.Filter("Bid", b.Id)
	}
	if data.Name != "" {
		qs.Filter("Name__istartswith", data.Name)
	}
	if data.Mime != "" {
		qs.Filter("Mime", strings.ToLower(data.Mime))
	}
	if data.Ext != "" {
		qs.Filter("Ext", strings.ToLower(data.Ext))
	}
	//查询总数
	count, _ := qs.Count()
	//查询列表
	page, offset, limit := paging.Calculation(data.Page, data.PageSize)
	list := make([]models.Object, data.PageSize)
	out := make([]map[string]interface{}, data.PageSize)
	_, err := qs.Offset(offset).Limit(limit).All(&list, "Created", "Bid", "Name", "Size", "Mime", "Ext")
	if err != nil {
		c.Data["json"] = jsonio.Fail(jsonio.ErrServerError, err.Error(), nil)
		return
	}
	bm := block.Instance(o)
	for _, item := range list {
		out = append(out, map[string]interface{}{
			"Created": item.Created.Format("2006-01-02 15:04:05"),
			"Block":   bm.GetNameById(item.Bid),
			"Name":    item.Name,
			"Size":    item.Size,
			"Mime":    item.Mime,
			"Ext":     item.Ext,
		})
	}
	c.Data["json"] = jsonio.Success(paging.PageUtilWithCount(int(count), page, limit, out), "")
}

// @Title 对象信息
// @Description 存储块信息
// @Param Id body int false "对象id"
// @Success 0     {}
// @Failure 50000 业务错误
// @Failure 50010 请求参数错误
// @router /info [post]
func (c *Object) Info() {
	var data struct {
		Id int
	}
	ok, info := jsonio.Input(c.Ctx.Input.RequestBody, &data)
	if !ok {
		c.Data["json"] = jsonio.Fail(jsonio.ErrParamError, info, nil)
		return
	}
	o := orm.NewOrm()
	om := &models.Object{
		Id:    data.Id,
		IsDel: 0,
	}
	err := o.Read(om, "Id", "IsDel")
	switch err {
	case nil:
		bm := block.Instance(o)
		c.Data["json"] = jsonio.Success(struct {
			Created    string
			Id         int
			Block      string
			Name       string
			Size       int
			Mime       string
			Ext        string
			Status     int8
			StatusText string
			PartId     int
		}{
			Created:    om.Created.Format("2006-01-02 15:04:05"),
			Id:         om.Id,
			Block:      bm.GetNameById(om.Bid),
			Name:       om.Name,
			Size:       om.Size,
			Mime:       om.Mime,
			Ext:        om.Ext,
			Status:     om.Status,
			StatusText: object.GetStatusFieldText("Status", om.Status),
			PartId:     om.PartId,
		}, "")
		return
	case orm.ErrNoRows:
		c.Data["json"] = jsonio.Fail(jsonio.ErrNotFound, "对象不存在", nil)
		return
	default:
		c.Data["json"] = jsonio.Fail(jsonio.ErrServerError, "服务器异常："+err.Error(), nil)
		return
	}
}

// @Title 删除对象
// @Description 删除对象
// @Param Id body int true "对象id"
// @Success 0     {}
// @Failure 50000 业务错误
// @Failure 50010 请求参数错误
// @router /remove [post]
func (c *Object) Remove() {
	var data struct {
		Id int
	}
	ok, info := jsonio.Input(c.Ctx.Input.RequestBody, &data)
	if !ok {
		c.Data["json"] = jsonio.Fail(jsonio.ErrParamError, info, nil)
		return
	}
	o := orm.NewOrm()
	ob := object.Instance(o)
	err := ob.Remove(data.Id)
	if err != nil {
		c.Data["json"] = jsonio.Fail(jsonio.ErrBusiness, err.Error(), nil)
		return
	}
	c.Data["json"] = jsonio.Success(nil, "对象删除成功")
}
